<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Model: NOT ESCAPE YET
 *
 * @author Zelic
 */
class Lib_Model {

    var $db;
    var $name;
    var $description;

    public function __construct() {
        $this->db = Lib_Registry::$data['db'];
        $part = explode('_', get_class($this));
        $this->name = strtolower($part[3]);
        $this->description = $this->getModel();
    }

    public function select($condition) {
        if (is_string($condition) && strcmp($condition, "all") == 0) {
            $query = "select * from $this->name";
            $data = array();
            $statement = $this->db->prepare($query);
            $statement->execute();
            $statement->setFetchMode(PDO::FETCH_ASSOC);
            while ($row = $statement->fetch()) {
                $data[] = $row;
            }
            return $data;
        } else if (is_array($condition)) {
            $query = "select * from $this->name";
            $data = array();
            if (isset($condition['fields'])) {
                $fields = $condition['fields'];
                $query = "select $fields from $this->name";
            }

            if (isset($condition['where'])) {
                $where = $condition['where'];
                $query = $query . " where $where";
            }

            if (isset($condition['orderBy'])) {
                $orderBy = "order by ";
                foreach ($orderBy as $item) {
                    $part = join(' ', $item);
                    $orderBy = $orderBy . $part . ', ';
                }
                $query = $query . " " . $orderBy;
            }

            if (isset($condition['groupBy'])) {
                $groupBy = join(',', $condition['groupBy']);
                $query = $query . " groupby $groupBy";
            }
            
            
            $statement = $this->db->prepare($query);
            $statement->execute();
            $statement->setFetchMode(PDO::FETCH_ASSOC);
            while ($row = $statement->fetch()) {
                $data[] = $row;
            }
            return $data;
        } else {
            return false;
        }
    }

    /**
     *
     * @param type $data: array of <field, value>
     * @return boolean 
     */
    public function insert($data) {
        if (is_array($data)) {
            $fields = array_keys($data);
            $values = array_values($data);
            $query = "insert into " . $this->name;
            if (is_array($fields)) {
                $fieldString = join(',', $fields);
                $query = $query . "(" . $fieldString . ")";
            }
            if (is_array($values)) {
                $newValues = array();
                foreach ($values as $item) {
                    if (is_string($item)) {
                        $item = $this->db->quote($item);
                    }
                    $newValues[] = $item;
                }
                $valueString = join(',', $newValues);
                $query = $query . ' values(' . $valueString . ')';
                $statement = $this->db->prepare($query);
                return $statement->execute();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function update($data) {
        $keys = $this->description['key'];

        $set = array();
        $where = array();
        foreach ($data as $field => $value) {
            if (in_array($field, $keys)) {
                $where[] = $field . '=' . ($this->db->quote($value));
            } else {
                $set[] = $field . '=' . ($this->db->quote($value));
            }
        }
        $setString = join(',', $set);
        $whereString = join(',', $where);
        $query = "update $this->name set $setString where $whereString";
        $statement = $this->db->prepare($query);
        return $statement->execute();
    }

    public function remove($condition) {
        if (is_array($condition)) {
            $where = array();
            foreach ($condition as $item) {
                $where[] = $item['field'] . '=' . $this->db->quote($item['value']);
            }
            $whereString = join(',', $where);
            $query = "delete from $this->name where $whereString";
            $statement = $this->db->prepare($query);
            return $statement->execute();
        } else {
            return false;
        }
    }

    public function getModel() {
        $query = "describe " . $this->name;
        $data = array();
        $statement = $this->db->prepare($query);
        $statement->execute();
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $statement->fetch()) {
            if (strcasecmp($row['Key'], 'PRI') == 0) {
                $data['key'][] = $row['Field'];
            } else {
                $data['data'][] = $row['Field'];
            }
        }
        return $data;
    }

}

?>
